// plugin/pages/auctionDetail/auctionDetail.js
var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp();
var timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    app.setNavtitle('竞拍详情');
    if(e&&e.id){
      that.setData({
        id:e.id
      })
    }
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
  },
  onShow:function(){
    var that = this;
    that.resuestDetail();
  },
  //显示分享模块
  showSharemodal(){
    var pageShare = this.selectComponent('.page-share'), 
    posterData={map:'applet_auction_detail_share',id:this.data.id};
    console.log(posterData)
    this.setData({
      posterData:posterData
    })
    pageShare.showSharemodal()
  },
  hideSharemodal(){
    var pageShare = this.selectComponent('.page-share');
    pageShare.hideSharemodal()
  },
  onUnload:function(){
    clearTimeout(timer);
  },
  onPullDownRefresh:function(){
    var that = this;
    that.resuestDetail();
  },
  update:function(){
    this.onPullDownRefresh();
  },
  resuestDetail: function () {
    var that = this;
    var data = {
      map: 'applet_auction_detail',
      id: that.data.id
    };
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          that.setData({
            detailInfo: res.data.data,
            hasData:1
          })
          var article = res.data.data.detail;
          // 富文本解析
          WxParse.wxParse('article', 'html', article, that, 5);
          clearTimeout(timer);
          that.countDownTime();
        } else {
          console.log(res.data)
          that.setData({
            hasData: 0
          })
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  // 活动剩余时间
  countDownTime: function () {
    var that = this;
    var detailInfo = that.data.detailInfo;
    var curTime = parseInt((new Date()).getTime() / 1000);
    var startTime = that.data.detailInfo.startTime;
    var endTime = that.data.detailInfo.endTime;
    // var startTime = new Date(that.data.detailInfo.startTime);
    // var endTime = new Date(that.data.detailInfo.endTime);
    // startTime = parseInt(startTime.getTime() / 1000);
    // endTime = parseInt(endTime.getTime() / 1000);
    var leftTime=0;
    if (startTime - curTime > 0) {
      leftTime = startTime - curTime;
    } else {
      if (detailInfo.status==1){
        detailInfo.status = 2;
        that.setData({
          detailInfo: detailInfo
        })
      }
      leftTime = endTime - curTime;
      if (leftTime<=0){
        leftTime=0;
      }
    }
    that.setData({
      leftTime: leftTime
    })
    if (leftTime <= 0) {
      if (detailInfo.status==2){
        that.resuestDetail();
      }
      clearTimeout(timer);
      return;
    }else{
      that.countDjs(leftTime);
      timer = setTimeout(function () {
        that.countDownTime();
      }, 1000)
    }
  },
  countDjs: function (leftTime) {
    var that = this;
    if (leftTime > 0) {
      var days = parseInt(leftTime / 60 / 60 / 24, 10); //计算剩余的天数 
      var hours = parseInt(leftTime / 60 / 60 % 24, 10); //计算剩余的小时 
      var minutes = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟 
      var seconds = parseInt(leftTime % 60, 10);//计算剩余的秒数 
      days = days < 10 ? '0' + days : days;
      // console.log(days);
      hours = hours < 10 ? '0' + hours : hours;
      minutes = minutes < 10 ? '0' + minutes : minutes;
      seconds = seconds < 10 ? '0' + seconds : seconds;
      that.setData({
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds
      })
      // leftTime--;
      if (days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
        clearTimeout(timer);
        leftTime = 0;
      }
    } else {
      that.setData({
        days: '00',
        hours: '00',
        minutes: '00',
        seconds: '00',
      })
      leftTime = 0;
      clearTimeout(timer);
      console.log("22222");
    }
    // that.data.intervarID = setTimeout(function () {
    //   that.countDjs(leftTime);
    // }, 1000)
  },
  backsy:function(){
    wx.redirectTo({
      url: '/subpages0/auctionpage/auctionIndex/auctionIndex',
    })
  },
  toauctionRecord:function(){
    var that = this;
    var id = that.data.detailInfo.id;
    wx.navigateTo({
      url: '/subpages0/auctionpage/auctionRecord/auctionRecord?id='+id,
    })
  },
  tosubmitOrder: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/subpages0/auctionpage/submitOrder/submitOrder?type=apply&id=' + id
    })
  },
  confirmFollow:function(){
    var that = this;
    wx.showModal({
      title: '',
      content: '确认跟拍吗？',
      confirmColor: '#1AAD16',
      success: function (res) {
        if (res.confirm) {
          var data = {
            map: 'applet_auction_follow',
            id: that.data.detailInfo.id
          }
          console.log(data);
          //发起请求，获取列表列表
          wx.showLoading({
            title: '加载中',
          })
          wx.request({
            url: app.globalData.requestUrl,
            data: data,
            success: function (res) {
              if (res.data.ec == 200) {
                app.errorTip(that, res.data.data.msg, 2000);
                that.resuestDetail();
              } else {
                app.errorTip(that, res.data.em, 2000);
              }
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var id = that.data.detailInfo.id;
    var title = that.data.detailInfo.title;
    app.getPoint(that);
    return {
      title: title,
      path: '/subpages0/auctionpage/auctionDetail/auctionDetail?enterfrom=share&id=' + id
    }
  }
})